/*
 * @lc app=leetcode.cn id=209 lang=cpp
 *
 * [209] 长度最小的子数组
 */
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cmath>

using namespace std;
// @lc code=start
class Solution {
public:
    int minSubArrayLen(int target, vector<int>& nums) {
        //滑动窗口发模板
        int sum = 0;
        int ret = INT32_MAX;
        int n = nums.size();
        for(int left = 0,right = 0;right < n;right++) {
            sum += nums[right];
            //左窗口右移
            while(sum - nums[left] >= target) {
                sum -= nums[left++];
            }
            if(sum >= target) {
                ret = min(ret,right - left + 1);
            }
        }
        return ret == INT32_MAX? 0:ret;
    }
};
// @lc code=end

